package it.rikesh.aip2011.client.component;

import it.rikesh.aip2011.client.BaseLayout;
import it.rikesh.aip2011.client.service.ActivityService;
import it.rikesh.aip2011.client.service.ActivityServiceAsync;
import it.rikesh.aip2011.shared.model.Hut;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

/**
 * @author  sunita
 */
public class ActivityLayout extends LayoutContainer {
    private static ActivityServiceAsync activityService = GWT.create(ActivityService.class);

    private Text txtName;
    private Text txtTitle;
    private Text txtDescription;
    private Text txtEquip;
    private Text txtTrail;
    private Text lblDescription;
    private Text lblEquip;
    private Text lblHut;
    private Text lblTrail;
    private List<Hut> huts;
    private HTML image;
    private FlexTable tableHut;
    private String id;
    
    public ActivityLayout(String name,boolean link){
        activityService.getActivityId(name, new AsyncCallback<String>() {

            public void onFailure(Throwable caught) {
                
                
            }

            public void onSuccess(String result) {
                id=result;
                init();
                getInfoActivity(id);
                
                addComponent();
            }
        });
        init();
        
    }
    
    public ActivityLayout(String id){
        init();
        getInfoActivity(id);
        
        addComponent();
    }
    
    /**
     * Questo metodo si occupa di inizializzare tutte le variabili necessari
     * Le proprietà del test, la dimensione dei caratteri etc.
     */
    public void init(){
        lblDescription=new Text("DESCRIZIONE");
        lblDescription.setStyleAttribute("color", "red");
        lblDescription.setStyleAttribute("text-align", "center");
        lblDescription.setStyleAttribute("font-weight", "bold");
        lblDescription.setStyleAttribute("font-size", "18px");
        lblEquip=new Text("EQUIPAGGIAMENTO");
        lblEquip.setStyleAttribute("color", "red");
        lblEquip.setStyleAttribute("text-align", "center");
        lblEquip.setStyleAttribute("font-weight", "bold");
        lblEquip.setStyleAttribute("font-size", "18px");
        lblHut=new Text("RIFUGI CHE OFFRONO QUESTA ATTIVITA'");
        lblHut.setStyleAttribute("color", "red");
        lblHut.setStyleAttribute("text-align", "center");
        lblHut.setStyleAttribute("font-weight", "bold");
        lblHut.setStyleAttribute("font-size", "18px");
        txtName=new Text();
        txtName.setStyleAttribute("color", "red");
        txtName.setStyleAttribute("text-align", "center");
        txtName.setStyleAttribute("font-weight", "bold");
        txtName.setStyleAttribute("font-size", "35px");
        txtName.setStyleAttribute("font-family", "Rock Salt");
        txtTitle=new Text();
        txtTitle.setStyleAttribute("color", "red");
        txtTitle.setStyleAttribute("text-align", "center");
        txtTitle.setStyleAttribute("font-weight", "bold");
        txtTitle.setStyleAttribute("font-size", "28px");
        txtDescription=new Text();
        txtDescription.setStyleAttribute("text-align", "center");
        txtEquip=new Text();
        txtEquip.setStyleAttribute("text-align", "center");
        lblTrail=new Text("PERCORSO DI QUESTA ATTIVITA'");
        lblTrail.setStyleAttribute("color", "red");
        lblTrail.setStyleAttribute("text-align", "center");
        lblTrail.setStyleAttribute("font-weight", "bold");
        lblTrail.setStyleAttribute("font-size", "18px");
        txtTrail=new Text();
        image=new HTML();
        tableHut=new FlexTable();
    }
    
    /**
     * Permettere di ottenere tutti dati nel datatstore relativo all'attività
     * @param id id del rifugio da visualizzare
     */
    private void getInfoActivity(String id){
        activityService.getHutActivity(id, new AsyncCallback<List<Hut>>() {
            
            public void onSuccess(List<Hut> result) {
                huts=new ArrayList<Hut>();
                for (int i=0;i<result.size();i++) {
                    huts.add(result.get(i));
                    Text h=new Text(result.get(i).getName());
                    h.setStyleAttribute("text-decoration", "underline");
                    h.setStyleAttribute("color", "blue");
                    h.addListener(Events.OnClick, new Listener<BaseEvent>() {
                        
                        public void handleEvent(BaseEvent be) {
                            BaseLayout.changeContent(new HutLayout(((Text)be.getSource()).getText(),true));
                        }
                    });
                    tableHut.setWidget(i, 0, h);
                }
                layout();
                
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        activityService.getActivityName(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtName.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        activityService.getActivityTitle(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtTitle.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        activityService.getActivityTrail(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtTrail.setText(result);
                txtTrail.setStyleAttribute("text-decoration", "underline");
                txtTrail.setStyleAttribute("color", "blue");
                txtTrail.addListener(Events.OnClick, new Listener<BaseEvent>() {
                    
                    public void handleEvent(BaseEvent be) {
                        BaseLayout.changeContent(new TrailLayout(((Text)be.getSource()).getText(),true));
                    }
                });
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        activityService.getActivityImg(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                image.setHTML("<center><img src='images/attivita/"+result+"' height='450px'/></center>");
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        activityService.getActivityEquip(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtEquip.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        activityService.getActivityDetail(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtDescription.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
    }
    
    private void addComponent(){
        add(txtName);
        add(txtTitle);
        add(image);
        add(lblDescription);
        add(txtDescription);
        add(lblEquip);
        add(txtEquip);
        add(lblHut);
        add(tableHut);
        add(lblTrail);
        add(txtTrail);
    }
}
